//
// Created by Huiyi on 2021/4/25.
//
#include "module_XExpand.h"

//网页_取端口A
int X_Network_GetPortA (const char*Url)
{
    string l_pr = X_Network_GetDomanNameA (Url, false, true);
    int l_inde = X_String_RfindA(l_pr.c_str(),":");
    if(l_inde!=-1)
    {
        return atoi(l_pr.substr(l_inde+1,l_pr.length()-l_inde-1).c_str());
    } else {
        if (strcmp(l_pr.substr(0,5).c_str(),"https")==0)
        {
            return 443;
        } else {
            return 80;
        }
    }
}

//网页_取域名A
CXTextA X_Network_GetDomanNameA (const char*Url,bool DellChild,bool ReservedPort,int SuffixNum)
{
    //仿写自精易模块
    string l_url (X_String_ToLowerA(Url).get());
    if (SuffixNum==-1)
    {
        if(strcmp(l_url.substr(l_url.length()-1,1).c_str(),"/")!=0)
        {
            l_url.append("/");
        }

        if (strcmp(l_url.substr(0,8).c_str(),"https://")==0)
        {
            l_url = X_String_GetMiddleTextA(l_url.c_str(),"https://","/").get();
        } else {
            if(l_url.find("https://")==string::npos)
            {
                l_url = "http://" + l_url;
            }
            l_url = X_String_GetMiddleTextA(l_url.c_str(),"http://","/").get();
        }
        if (DellChild)
        {
            CXVector<CXTextA> l_two;
            int l_num = X_String_SplitWithStlA (l_url.c_str(),".",l_two);
            if(l_num>1)
            {
                l_url = "";
                l_url.append (l_two [l_num-2].get()).append(".").append (l_two[l_num-1].get());
            }
        }
    } else {
        l_url = X_String_SubreplaceA(l_url.c_str(),"http://","");
        l_url = X_String_SubreplaceA(l_url.c_str(),"https://","");
        int l_inde = l_url.find('/');
        if(l_inde!=string::npos)
        {
            l_url = l_url.substr(0,l_inde);
        }
        int l_urllen = l_url.length();
        int l_PointAfter = l_urllen;
        for (int i=0;i<SuffixNum;i++)
        {
            l_PointAfter = X_String_RfindA (l_url.c_str(),".",l_PointAfter);
        }
        int l_BeforePoint = X_String_RfindA (l_url.c_str(),".",l_PointAfter);
        if (DellChild)
        {
            l_url = X_String_GetRightA (l_url.c_str(),l_urllen - l_BeforePoint);
        } else {
            int l_BeforePointBegin;
            if (l_BeforePoint != string::npos)//至少二级域名
            {
                l_BeforePointBegin = X_String_RfindA(l_url.c_str(),".",l_BeforePoint);
                if(l_BeforePointBegin != string::npos)//至少三级域名
                {
                    l_url = X_String_GetRightA (l_url.c_str(),l_urllen-l_BeforePointBegin);
                }
            }
        }
    }
    if(ReservedPort)
    {
        return l_url;
    } else {
        int l_index = l_url.find(":");
        if (l_index!=string::npos)
        {
            return X_String_GetLeftA(l_url.c_str(),l_index);
        } else {
            return l_url;
        }
    }
}

//解析所有头A
int X_Network_private_GetAllHeadA (const char*head,httplib::Headers& hds)
{
    hds.clear();
    CXVector<CXTextA> has;
    X_String_SplitWithStlA(head,"\n",has);
    for (int i=0;i<has.size();i++)
    {
        string lstr = has[i].get ();
        int linde = lstr.find(": ");
        if(linde!=string::npos)
        {
            CXTextA lkey=lstr.substr(0,linde);
            CXTextA lvalue=lstr.substr(linde+2,lstr.length()-linde-2);
            hds.insert(make_pair(lkey.get(),lvalue.get()));
        }
    }
    return hds.size();
}

//寻找头A
string X_Network_private_FindHeadA(const httplib::Headers hds,const char* key)
{
    string l_ret;
    int lc = hds.count(key);
    auto lca = hds.find(key);
    while(lc)
    {
        l_ret.append (lca->second.c_str());
        ++lca;
        --lc;
        if(lc)
        {
            l_ret.append(", ");
        }
    }
    return l_ret;
}
/*
//网页_访问A
CXTextA X_Network_AccessA (const char* Url,int Type,const char*SubmitData,int rCode,const char*Head,const char*cookies)
{
    rCode = 0;
    string l_url = Url;
    string l_path;
    if (strcmp(l_url.substr(0,7).c_str(),"http://")==0
        || strcmp(l_url.substr(0,8).c_str(),"https://")==0)
    {
        int l_c = l_url.find('/',9);
        if (l_c==string::npos)
        {
            l_path="/";
        } else {
            int l_inde = l_url.length()-l_c;
            l_path=l_url.substr(l_c,l_inde);
            l_url=l_url.substr(0,l_url.length()-l_inde);
        }
    }else{return "";}
    httplib::Headers l_hend;
    X_Network_private_GetAllHeadA(Head,l_hend);
    string l_Content_Type = X_Network_private_FindHeadA(l_hend,"Content-Type");
    if(strcmp(l_Content_Type.c_str(),"")==0){
        l_Content_Type="application/x-www-from-urlencoded";
        l_hend.insert(make_pair("Content-Type","application/x-www-from-urlencoded"));
    }
    if(strcmp(X_Network_private_FindHeadA(l_hend,"User-Agent").c_str(),"")==0){
        l_hend.insert(make_pair("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"));
    }
    if (strcmp(cookies,"")!=0)
    {
    }
    string rets;
    httplib::Client clent (l_url.c_str());
    CXTextA l_rehead="";
    if (Type == 0)
    {
        auto l_cres = clent.Get(l_path.c_str(),l_hend);
        rCode = l_cres->status;
        rets = l_cres->body;
        string l_hcot = l_cres->reason;
        xc_log("l_hcot",l_hcot.c_str());

    } else if (Type == 1) {
        auto l_cres = clent.Post(l_path.c_str(),SubmitData,l_Content_Type.c_str());
        rCode = l_cres->status;
        rets = l_cres->body;
    } else {
        return "";
    }

    return rets;
}*/

//网页协议头取值A
CXTextA X_Network_GetHeadsValueA(const char*Head,const char*Key)
{
    CXTextA ret = "";
    CXVector<CXTextA> has;
    int keylen = strlen(Key);
    X_String_SplitWithStlA(Head,"\n",has);
    for (int i=0;i<has.size();i++)
    {
        string ltstr = has[i].get();
        if(strcmp(ltstr.substr(0,keylen).c_str(),Key)==0)
        {
            int linte = ltstr.find(": ");
            if(linte!=string::npos)
            {
                linte+=2;
                ret = ltstr.substr(linte,ltstr.length()-linte);
                break;
            }
        }
    }
    return ret;
}

//取单条协议头名A
CXTextA X_Network_GetHeadNameA(const char*Head)
{
    string lhead (Head);
    CXTextA ret="";
    int linde = lhead.find(":");
    if(linde != string::npos)
    {
        ret = X_String_GetLeftA(Head,linde);
    }
    return X_String_TrimA(ret);
}

//取单条协议头值A
CXTextA X_Network_GetHeadValueA(const char*Head)
{
    string lhead (Head);
    CXTextA ret="";
    int linde = lhead.find(":");
    if(linde != string::npos)
    {
        ret = X_String_GetRightA(Head,lhead.length()-linde-1);
    }
    return X_String_TrimA(ret);
}


